package com.xc.find;

public class BinarFind {
    public static void main(String[] args) {
        int nums[] = {1, 2, 35, 64, 222};
//        int bin = bin(a, 0, a.length - 1, 64);
        System.out.println(BinarFind.search(nums, 2));

//        System.out.println(bin);


    }

    public static   int search(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;

        while (left<=right) {
            int mid = (left + right) / 2;
            if (nums[mid] < target) {
                  left=left+1;
            } else if (nums[mid] == target) {
                return mid;
            } else {
                 right = right - 1;
            }
            }

            return -1;
        }


        public static int bin ( int a[], int left, int right, int zhi){
            int mid = (left + right) / 2;

            if (a != null) {
                if (zhi > a[mid]) {
                    return bin(a, mid + 1, right, zhi);
                } else if (zhi < a[mid]) {
                    return bin(a, left, mid - 1, zhi);
                } else {
                    return a[mid];
                }
            }
            return -1;
        }
    }
